<?php
namespace app\daohang\controller;

use app\common\controller\Front;

class Publish extends Front
{
    //继承上级
    public function _initialize()
    {
        parent::_initialize();
    }
    
    //免费收录
    public function index()
    {
        $this->assign([
            'seoTitle'       => daohangSeo(config('daohang.publish_index_title')),
            'seoKeywords'    => daohangSeo(config('daohang.publish_index_keywords')),
            'seoDescription' => daohangSeo(config('daohang.publish_index_description')),
        ]);
        return $this->fetch();
    }
    
    //快速收录
    public function fast()
    {
        $this->assign([
            'scoreWeb'       => intval(config('daohang.score_fast_web')),
            'scoreMp'        => intval(config('daohang.score_fast_mp')),
            'scoreMini'      => intval(config('daohang.score_fast_mini')),
            'seoTitle'       => daohangSeo(config('daohang.publish_fast_title')),
            'seoKeywords'    => daohangSeo(config('daohang.publish_fast_keywords')),
            'seoDescription' => daohangSeo(config('daohang.publish_fast_description')),
        ]);
        return $this->fetch();
    }
    
    //免费收录网站
    public function web()
    {
        $this->assign([
            'seoTitle'       => daohangSeo(config('daohang.publish_web_title')),
            'seoKeywords'    => daohangSeo(config('daohang.publish_web_keywords')),
            'seoDescription' => daohangSeo(config('daohang.publish_web_description')),
        ]);
        return $this->fetch();
    }
    
    //免费收录公众号
    public function mp()
    {
        $this->assign([
            'seoTitle'       => daohangSeo(config('daohang.publish_mp_title')),
            'seoKeywords'    => daohangSeo(config('daohang.publish_mp_keywords')),
            'seoDescription' => daohangSeo(config('daohang.publish_mp_description')),
        ]);
        return $this->fetch();
    }
    
    //免费收录小程序
    public function mini()
    {
        $this->assign([
            'seoTitle'       => daohangSeo(config('daohang.publish_mini_title')),
            'seoKeywords'    => daohangSeo(config('daohang.publish_mini_keywords')),
            'seoDescription' => daohangSeo(config('daohang.publish_mini_description')),
        ]);
        return $this->fetch();
    }
    
    //免费发布网站、公众号、小程序
    public function save()
    {
        //获取表单数据
        $data = $this->post();
        $data['info_type']     = 'index';
        $data['info_status']   = 'hidden';
        //保存至数据库
        if( !DcArrayResult(daohangSave($data,false)) ){
		    $this->error(\daicuo\Info::getError());
        }
        $this->success(lang('success'));
    }
    
    //免审发布网站
    public function webSave()
    {
        //用户组与积分处理
        $this->authFast(config('daohang.score_fast_web'));
        //合并表单数据
        $data = DcArrayArgs([
            'info_module'   => 'daohang',
            'info_controll' => 'web',
            'info_action'   => 'index',
            'info_status'   => 'normal',
            'info_type'     => 'fast',
        ], $this->post());
        
        //保存至数据库
        if( !DcArrayResult(daohangSave($data)) ){
		    $this->error(\daicuo\Info::getError());
        }
        
        //扣除积分
        userScoreDec($this->site['user']['user_id'], config('daohang.score_fast_web'));
        
        //返回结果
        $this->success(lang('success'));
    }
    
    //免审发布公从号
    public function mpSave()
    {
        //用户组与积分处理
        $this->authFast(config('daohang.score_fast_mp'));
        //合并表单数据
        $data = DcArrayArgs([
            'info_module'   => 'daohang',
            'info_controll' => 'mp',
            'info_action'   => 'index',
            'info_status'   => 'normal',
            'info_type'     => 'fast',
        ], $this->post());
        
        //保存至数据库
        if( !DcArrayResult(daohangSave($data)) ){
		    $this->error(\daicuo\Info::getError());
        }
        
        //扣除积分
        userScoreDec($this->site['user']['user_id'], config('daohang.score_fast_mp'));
        
        //返回结果
        $this->success(lang('success'));
    }
    
    //免审发布小程序
    public function miniSave()
    {
        //用户组与积分处理
        $this->authFast(config('daohang.score_fast_mini'));
        //合并表单数据
        $data = DcArrayArgs([
            'info_module'   => 'daohang',
            'info_controll' => 'mini',
            'info_action'   => 'index',
            'info_status'   => 'normal',
            'info_type'     => 'fast',
        ], $this->post());
        
        //保存至数据库
        if( !DcArrayResult(daohangSave($data)) ){
		    $this->error(\daicuo\Info::getError());
        }
        
        //扣除积分
        userScoreDec($this->site['user']['user_id'], config('daohang.score_fast_mini'));
        
        //返回结果
        $this->success(lang('success'));
    }
    
    //更新网站发布
    private function webupdate()
    {
        $url = input('post.info_referer/s');
        if(!$url){
            $this->error('请输入待收录的网址');
        }
        $html = DcCurl('windows', 10, $url, '', 'https://www.daicuo.org');
        if(!$html){
            $this->error('您输入的网址不正确，请重新输入！');
        }
        //组合表单数据
        $data = [];
        $data['info_title']       = trim(DcPregMatch('<title>([\s\S]*?)<\/title>', $html));
        $data['info_keywords']    = trim(str_replace('，',',',DcPregMatch('<meta name="keywords" content="([\s\S]*?)"', $html)));
        $data['info_description'] = trim(DcPregMatch('<meta name="description" content="([\s\S]*?)"', $html));
    }
    
    //接收POST数据
    private function post()
    {
        $data = [];
        //分类必填
        $data['category_id']      = input('post.category_id/a');
        if(!$data['category_id']){
            $this->error( lang('dh_error_category_empty') );
        }
        $data['tag_name']         = input('post.tag_name/s');
        $data['info_name']        = input('post.info_name/s','未填写标题');
        $data['info_slug']        = input('post.info_slug/s','');
        $data['image_ico']        = input('post.image_ico/s','');
        $data['info_content']     = input('post.info_content/s','未填写');
        $data['info_excerpt']     = input('post.info_excerpt/s','');
        $data['info_referer']     = input('post.info_referer/s','');
        $data['info_title']       = input('post.info_title/s','');
        $data['info_keywords']    = input('post.info_keywords/s','');
        $data['info_description'] = input('post.info_description/s','');
        //图片相关
        $data['image_level']      = input('post.image_level/s','');
        $data['image_qrcode']     = input('post.image_qrcode/s','');
        $data['image_screen']     = str_replace(',',';',input('post.image_screen/s',''));
        $data['term_id']          = [];
        $data['info_views']       = rand(99,999);
        $data['info_hits']        = rand(9,99);
        $data['info_user_id']     = $this->site['user']['user_id'];
        $data['info_action']      = 'index';
        $data['info_controll']    = input('post.info_controll/s','web');
        $data['info_module']      = 'daohang';
        return $data;
    }
    
    //手动验证快审发布权限与积分处理权限
    private function authFast($scoreConfig=0)
    {
        //验证规则
        $this->auth['rule'] = 'daohang/publish/'.$this->site['action'];
        //VIP用户组
        if ( \daicuo\Auth::check($this->auth['rule'], $this->site['user']['user_capabilities'], $this->site['user']['user_caps']) ) {
            return true;
        }
        //积分扣除配置（小于1不启用积分发布功能）
        if( $scoreConfig < 1 ){
            return true;
        }
        //积分不足
        if($this->site['user']['user_score'] < $scoreConfig){
            $this->error( DcError(lang('dh_error_fast_save')), 'user/center/index');
        }
    }
}